﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class blueQuilt : MonoBehaviour
{
    // 行走速度
    private int walkSpeed = 7; 
    private int jumpSpeed = 8;
    // 表示向左还是向右 1:向右 -1:向左 0:不动  默认为不动
    private int rightOrleft = 0;
    // 表示是否跳起  这边可以有多个状态，可以让人物动画更协调一些
    private bool isJump = false;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        // 移动
        if(Input.GetKey(KeyCode.RightArrow))
        {
            // 跳跃
            rightOrleft = 1;
        }
        else
        {
            if(Input.GetKey(KeyCode.LeftArrow))
            {
                rightOrleft = -1;
            }
        }

        if(Input.GetKey(KeyCode.UpArrow))
        {
            isJump = true;
        }

        move(rightOrleft);
        jump(isJump);
        rightOrleft = 0;
        isJump = false;
    }

    //与collide碰撞检测
    private void OnCollisionEnter2D(Collision2D other) {
        if(other.transform.tag == "harmfulting")
        {
            dead();
        }
    } 
    
    //和触发器碰撞
    private void OnTriggerEnter2D(Collider2D other) {
        if(other.transform.tag == "harmfulting")
        {
            dead();
        }
    }

    // 跳跃
    void jump(bool s) 
    {
       if(s)
        {
            gameObject.transform.position = new Vector3(gameObject.transform.position.x,
                                                        gameObject.transform.position.y + jumpSpeed * Time.deltaTime,0);
        }
    }
    // 行走
    void move(int s)
    {
        float x = 0;
        switch(s) {
            // 缺少改变人物朝向的设置
            case 1:  gameObject.transform.position = new Vector3(gameObject.transform.position.x + walkSpeed * Time.deltaTime,
                                    gameObject.transform.position.y,0);
                    //  修改朝向
                     x = transform.localScale.x;
                     if(transform.localScale.x < 0)
                     {
                         x = -x;
                     }    
                     transform.localScale = new Vector3(x,transform.localScale.y,transform.localScale.z);
                     break;
            case 0:  break;
            case -1: gameObject.transform.position = new Vector3(gameObject.transform.position.x - walkSpeed * Time.deltaTime,
                                    gameObject.transform.position.y,0);
                     //  修改朝向
                     x = transform.localScale.x;
                     if(transform.localScale.x > 0)
                     {
                         x = -x;
                     }                   
                     transform.localScale = new Vector3(x,transform.localScale.y,transform.localScale.z);
                     break;
        }
    }

    // 死亡函数
    void dead()
    {
       Destroy(gameObject);
    }
}
